package com.tm.seurity.handler;

import com.tm.result.Result;
import com.tm.result.ResultCode;
import com.tm.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by lichuan on 2022-09-24.
 */
@Component
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        //TODO 退出登陆
        System.out.println("退出登陆");
        String authorization = httpServletRequest.getHeader("Authorization");
        String[] split = authorization.split(" ");
        redisTemplate.delete(split[1]);
        ResponseUtil.out(httpServletResponse, Result.SUCCESS(ResultCode.LOGOUT_SUCCESS));
    }
}
